package com.example.socialapp.ui.topic

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.FavoriteBorder
import androidx.compose.material.icons.filled.MoreVert
import androidx.compose.material.icons.filled.Share
import androidx.compose.material.icons.outlined.Add
import androidx.compose.material.icons.outlined.InsertComment
import androidx.compose.material.icons.outlined.Notifications
import androidx.compose.material.icons.outlined.NotificationsActive
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import kotlin.math.absoluteValue
import kotlin.random.Random

/**
 * 话题详情界面
 *
 * @param topicId 话题ID
 * @param onNavigateBack 返回上一界面回调
 * @param onNavigateToUserProfile 导航到用户资料界面回调
 * @param onNavigateToPostDetail 导航到动态详情界面回调
 */
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun TopicDetailScreen(
    topicId: String,
    onNavigateBack: () -> Unit,
    onNavigateToUserProfile: (String) -> Unit = {},
    onNavigateToPostDetail: (String) -> Unit = {}
) {
    // 在实际应用中应从ViewModel获取话题数据
    val topic = remember { generateSampleTopic(topicId) }
    val posts = remember { generateSamplePosts(topic, 10) }
    val relatedTopics = remember { generateRelatedTopics(topic) }
    val trendingUsers = remember { generateTrendingUsers(5) }
    
    var selectedTabIndex by remember { mutableIntStateOf(0) }
    val tabs = listOf("热门", "最新", "精选")
    
    var isFollowing by remember { mutableStateOf(false) }
    var isNotificationEnabled by remember { mutableStateOf(false) }
    
    Scaffold(
        topBar = {
            TopAppBar(
                title = { Text("话题详情") },
                navigationIcon = {
                    IconButton(onClick = onNavigateBack) {
                        Icon(
                            imageVector = Icons.Default.ArrowBack,
                            contentDescription = "返回"
                        )
                    }
                },
                actions = {
                    IconButton(onClick = { /* 分享话题 */ }) {
                        Icon(
                            imageVector = Icons.Default.Share,
                            contentDescription = "分享"
                        )
                    }
                    IconButton(onClick = { /* 更多选项 */ }) {
                        Icon(
                            imageVector = Icons.Default.MoreVert,
                            contentDescription = "更多选项"
                        )
                    }
                },
                colors = TopAppBarDefaults.topAppBarColors(
                    containerColor = MaterialTheme.colorScheme.primaryContainer,
                    titleContentColor = MaterialTheme.colorScheme.onPrimaryContainer
                )
            )
        }
    ) { innerPadding ->
        LazyColumn(
            modifier = Modifier
                .fillMaxSize()
                .padding(innerPadding)
        ) {
            // 话题信息卡片
            item {
                TopicInfoCard(
                    topic = topic,
                    isFollowing = isFollowing,
                    isNotificationEnabled = isNotificationEnabled,
                    onFollowClick = { isFollowing = !isFollowing },
                    onNotificationClick = { isNotificationEnabled = !isNotificationEnabled }
                )
                
                Spacer(modifier = Modifier.height(8.dp))
            }
            
            // 参与用户
            item {
                TrendingUsersSection(
                    users = trendingUsers,
                    onUserClick = onNavigateToUserProfile
                )
                
                Spacer(modifier = Modifier.height(8.dp))
                Divider()
            }
            
            // 相关话题
            item {
                RelatedTopicsSection(
                    topics = relatedTopics,
                    onTopicClick = { /* 导航到其他话题 */ }
                )
                
                Spacer(modifier = Modifier.height(8.dp))
                Divider()
            }
            
            // 标签行
            item {
                Column {
                    TabRow(selectedTabIndex = selectedTabIndex) {
                        tabs.forEachIndexed { index, title ->
                            Tab(
                                selected = selectedTabIndex == index,
                                onClick = { selectedTabIndex = index },
                                text = { Text(title) }
                            )
                        }
                    }
                    
                    Spacer(modifier = Modifier.height(8.dp))
                }
            }
            
            // 话题内容列表
            if (posts.isEmpty()) {
                item {
                    EmptyContent(text = "暂无相关动态")
                }
            } else {
                items(posts) { post ->
                    PostItem(
                        post = post,
                        onPostClick = { onNavigateToPostDetail(post.id) },
                        onAuthorClick = { onNavigateToUserProfile(post.authorId) }
                    )
                    Divider()
                }
            }
        }
    }
}

/**
 * 话题信息卡片
 */
@Composable
private fun TopicInfoCard(
    topic: Topic,
    isFollowing: Boolean,
    isNotificationEnabled: Boolean,
    onFollowClick: () -> Unit,
    onNotificationClick: () -> Unit
) {
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp),
        elevation = CardDefaults.cardElevation(defaultElevation = 2.dp),
        colors = CardDefaults.cardColors(
            containerColor = MaterialTheme.colorScheme.surface
        )
    ) {
        Column(
            modifier = Modifier.padding(16.dp),
            horizontalAlignment = Alignment.CenterHorizontally
        ) {
            // 话题图标
            Box(
                modifier = Modifier
                    .size(80.dp)
                    .clip(RoundedCornerShape(16.dp))
                    .background(getColorForTopic(topic)),
                contentAlignment = Alignment.Center
            ) {
                Text(
                    text = "#",
                    style = MaterialTheme.typography.headlineLarge,
                    color = Color.White
                )
            }
            
            Spacer(modifier = Modifier.height(16.dp))
            
            // 话题名称
            Text(
                text = "#${topic.name}",
                style = MaterialTheme.typography.headlineSmall,
                fontWeight = FontWeight.Bold
            )
            
            Spacer(modifier = Modifier.height(8.dp))
            
            // 话题描述
            Text(
                text = topic.description,
                style = MaterialTheme.typography.bodyMedium,
                textAlign = TextAlign.Center,
                color = MaterialTheme.colorScheme.onSurfaceVariant
            )
            
            Spacer(modifier = Modifier.height(16.dp))
            
            // 话题统计数据
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceEvenly
            ) {
                StatItem(count = topic.postsCount, label = "动态")
                StatItem(count = topic.viewsCount, label = "浏览")
                StatItem(count = topic.followersCount, label = "关注")
            }
            
            Spacer(modifier = Modifier.height(16.dp))
            
            // 操作按钮
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.spacedBy(8.dp)
            ) {
                Button(
                    onClick = onFollowClick,
                    modifier = Modifier.weight(1f),
                    colors = ButtonDefaults.buttonColors(
                        containerColor = if (isFollowing) {
                            MaterialTheme.colorScheme.surfaceVariant
                        } else {
                            MaterialTheme.colorScheme.primary
                        }
                    )
                ) {
                    Icon(
                        imageVector = Icons.Outlined.Add,
                        contentDescription = null
                    )
                    Spacer(modifier = Modifier.width(4.dp))
                    Text(text = if (isFollowing) "已关注" else "关注")
                }
                
                OutlinedButton(
                    onClick = onNotificationClick,
                    modifier = Modifier.weight(1f)
                ) {
                    Icon(
                        imageVector = if (isNotificationEnabled) 
                            Icons.Outlined.NotificationsActive 
                        else 
                            Icons.Outlined.Notifications,
                        contentDescription = null
                    )
                    Spacer(modifier = Modifier.width(4.dp))
                    Text(text = if (isNotificationEnabled) "已订阅" else "订阅")
                }
            }
        }
    }
}

/**
 * 热门用户部分
 */
@Composable
private fun TrendingUsersSection(
    users: List<User>,
    onUserClick: (String) -> Unit
) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp)
    ) {
        Text(
            text = "话题参与者",
            style = MaterialTheme.typography.titleMedium,
            fontWeight = FontWeight.Bold
        )
        
        Spacer(modifier = Modifier.height(8.dp))
        
        LazyRow(
            contentPadding = PaddingValues(4.dp),
            horizontalArrangement = Arrangement.spacedBy(16.dp)
        ) {
            items(users) { user ->
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.clickable { onUserClick(user.id) }
                ) {
                    // 用户头像
                    Box(
                        modifier = Modifier
                            .size(56.dp)
                            .clip(CircleShape)
                            .background(getColorForUser(user)),
                        contentAlignment = Alignment.Center
                    ) {
                        Text(
                            text = user.nickname.first().toString(),
                            style = MaterialTheme.typography.titleMedium,
                            color = Color.White
                        )
                    }
                    
                    Spacer(modifier = Modifier.height(4.dp))
                    
                    // 用户名
                    Text(
                        text = user.nickname,
                        style = MaterialTheme.typography.bodySmall,
                        maxLines = 1,
                        overflow = TextOverflow.Ellipsis
                    )
                }
            }
        }
    }
}

/**
 * 相关话题部分
 */
@Composable
private fun RelatedTopicsSection(
    topics: List<Topic>,
    onTopicClick: (String) -> Unit
) {
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .padding(16.dp)
    ) {
        Text(
            text = "相关话题",
            style = MaterialTheme.typography.titleMedium,
            fontWeight = FontWeight.Bold
        )
        
        Spacer(modifier = Modifier.height(8.dp))
        
        LazyRow(
            contentPadding = PaddingValues(4.dp),
            horizontalArrangement = Arrangement.spacedBy(8.dp)
        ) {
            items(topics) { topic ->
                TopicChip(
                    topic = topic,
                    onClick = { onTopicClick(topic.id) }
                )
            }
        }
    }
}

/**
 * 话题标签
 */
@Composable
private fun TopicChip(
    topic: Topic,
    onClick: () -> Unit
) {
    Card(
        modifier = Modifier.clickable(onClick = onClick),
        shape = RoundedCornerShape(16.dp),
        colors = CardDefaults.cardColors(
            containerColor = MaterialTheme.colorScheme.surfaceVariant
        )
    ) {
        Row(
            verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier.padding(horizontal = 12.dp, vertical = 8.dp)
        ) {
            Text(
                text = "#${topic.name}",
                style = MaterialTheme.typography.bodyMedium
            )
        }
    }
}

/**
 * 统计项
 */
@Composable
private fun StatItem(count: Int, label: String) {
    Column(
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        Text(
            text = formatCount(count),
            style = MaterialTheme.typography.titleMedium,
            fontWeight = FontWeight.Bold
        )
        Text(
            text = label,
            style = MaterialTheme.typography.bodyMedium,
            color = MaterialTheme.colorScheme.onSurfaceVariant
        )
    }
}

/**
 * 格式化数字，超过1000显示为K
 */
private fun formatCount(count: Int): String {
    return when {
        count < 1000 -> count.toString()
        count < 1000000 -> String.format("%.1fK", count / 1000.0)
        else -> String.format("%.1fM", count / 1000000.0)
    }
}

/**
 * 动态项
 */
@Composable
private fun PostItem(
    post: Post,
    onPostClick: () -> Unit,
    onAuthorClick: () -> Unit
) {
    var isLiked by remember { mutableStateOf(post.isLiked) }
    
    Card(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 16.dp, vertical = 8.dp)
            .clickable(onClick = onPostClick),
        elevation = CardDefaults.cardElevation(defaultElevation = 0.dp),
        shape = RoundedCornerShape(8.dp)
    ) {
        Column(modifier = Modifier.padding(16.dp)) {
            // 作者信息
            Row(
                verticalAlignment = Alignment.CenterVertically,
                modifier = Modifier.clickable(onClick = onAuthorClick)
            ) {
                // 头像
                Box(
                    modifier = Modifier
                        .size(40.dp)
                        .clip(CircleShape)
                        .background(getColorForName(post.authorName)),
                    contentAlignment = Alignment.Center
                ) {
                    Text(
                        text = post.authorName.first().toString(),
                        style = MaterialTheme.typography.bodyLarge,
                        color = Color.White
                    )
                }
                
                Spacer(modifier = Modifier.width(8.dp))
                
                Column {
                    Text(
                        text = post.authorName,
                        style = MaterialTheme.typography.bodyLarge,
                        fontWeight = FontWeight.Bold
                    )
                    Text(
                        text = post.formattedTime,
                        style = MaterialTheme.typography.bodySmall,
                        color = MaterialTheme.colorScheme.outline
                    )
                }
            }
            
            Spacer(modifier = Modifier.height(8.dp))
            
            // 动态内容
            Text(
                text = post.content,
                style = MaterialTheme.typography.bodyMedium,
                maxLines = 3,
                overflow = TextOverflow.Ellipsis
            )
            
            if (post.hasImage) {
                Spacer(modifier = Modifier.height(8.dp))
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(150.dp)
                        .clip(RoundedCornerShape(8.dp))
                        .background(MaterialTheme.colorScheme.secondaryContainer),
                    contentAlignment = Alignment.Center
                ) {
                    Text(
                        text = "图片内容",
                        style = MaterialTheme.typography.bodyLarge,
                        color = MaterialTheme.colorScheme.onSecondaryContainer
                    )
                }
            }
            
            Spacer(modifier = Modifier.height(8.dp))
            
            // 操作栏
            Row(
                modifier = Modifier.fillMaxWidth(),
                horizontalArrangement = Arrangement.SpaceBetween
            ) {
                // 点赞
                Row(
                    verticalAlignment = Alignment.CenterVertically,
                    modifier = Modifier.clickable { isLiked = !isLiked }
                ) {
                    Icon(
                        imageVector = if (isLiked) Icons.Default.Favorite else Icons.Default.FavoriteBorder,
                        contentDescription = "点赞",
                        tint = if (isLiked) Color.Red else MaterialTheme.colorScheme.onSurface,
                        modifier = Modifier.size(20.dp)
                    )
                    Spacer(modifier = Modifier.width(4.dp))
                    Text(
                        text = (post.likes + if (isLiked && !post.isLiked) 1 else 0).toString(),
                        style = MaterialTheme.typography.bodyMedium
                    )
                }
                
                // 评论
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Icon(
                        imageVector = Icons.Outlined.InsertComment,
                        contentDescription = "评论",
                        modifier = Modifier.size(20.dp)
                    )
                    Spacer(modifier = Modifier.width(4.dp))
                    Text(
                        text = post.comments.toString(),
                        style = MaterialTheme.typography.bodyMedium
                    )
                }
                
                // 分享
                Row(verticalAlignment = Alignment.CenterVertically) {
                    Icon(
                        imageVector = Icons.Default.Share,
                        contentDescription = "分享",
                        modifier = Modifier.size(20.dp)
                    )
                    Spacer(modifier = Modifier.width(4.dp))
                    Text(
                        text = post.shares.toString(),
                        style = MaterialTheme.typography.bodyMedium
                    )
                }
            }
        }
    }
}

/**
 * 空内容提示
 */
@Composable
private fun EmptyContent(text: String) {
    Box(
        modifier = Modifier
            .fillMaxWidth()
            .height(200.dp),
        contentAlignment = Alignment.Center
    ) {
        Text(
            text = text,
            style = MaterialTheme.typography.bodyLarge,
            color = MaterialTheme.colorScheme.outline
        )
    }
}

/**
 * 根据话题生成颜色
 */
private fun getColorForTopic(topic: Topic): Color {
    val colors = listOf(
        Color(0xFF6200EA), // 深紫色
        Color(0xFF2962FF), // 深蓝色
        Color(0xFF00BFA5), // 青色
        Color(0xFFFFAB00), // 琥珀色
        Color(0xFFD50000)  // 红色
    )
    
    return colors[topic.name.hashCode().mod(colors.size).absoluteValue]
}

/**
 * 根据用户生成颜色
 */
private fun getColorForUser(user: User): Color {
    val colors = listOf(
        Color(0xFF6200EA), // 深紫色
        Color(0xFF2962FF), // 深蓝色
        Color(0xFF00BFA5), // 青色
        Color(0xFFFFAB00), // 琥珀色
        Color(0xFFD50000)  // 红色
    )
    
    return colors[user.nickname.hashCode().mod(colors.size).absoluteValue]
}

/**
 * 根据名称生成颜色
 */
private fun getColorForName(name: String): Color {
    val colors = listOf(
        Color(0xFF6200EA), // 深紫色
        Color(0xFF2962FF), // 深蓝色
        Color(0xFF00BFA5), // 青色
        Color(0xFFFFAB00), // 琥珀色
        Color(0xFFD50000)  // 红色
    )
    
    return colors[name.hashCode().mod(colors.size).absoluteValue]
}

/**
 * 话题数据类
 */
data class Topic(
    val id: String,
    val name: String,
    val description: String,
    val postsCount: Int,
    val viewsCount: Int,
    val followersCount: Int
)

/**
 * 用户数据类
 */
data class User(
    val id: String,
    val nickname: String,
    val postsCount: Int = 0
)

/**
 * 动态数据类
 */
data class Post(
    val id: String,
    val authorId: String,
    val authorName: String,
    val content: String,
    val timestamp: Long,
    val likes: Int,
    val comments: Int,
    val shares: Int,
    val hasImage: Boolean,
    val isLiked: Boolean
) {
    val formattedTime: String
        get() {
            val now = System.currentTimeMillis()
            val diff = now - timestamp
            
            return when {
                diff < 60 * 1000 -> "刚刚"
                diff < 60 * 60 * 1000 -> "${diff / (60 * 1000)}分钟前"
                diff < 24 * 60 * 60 * 1000 -> "${diff / (60 * 60 * 1000)}小时前"
                else -> {
                    val days = diff / (24 * 60 * 60 * 1000)
                    when {
                        days < 30 -> "${days}天前"
                        else -> "${days / 30}月前"
                    }
                }
            }
        }
}

/**
 * 生成示例话题
 */
private fun generateSampleTopic(topicId: String): Topic {
    val topicsMap = mapOf(
        "1" to Topic(
            id = "1",
            name = "旅行",
            description = "分享旅行见闻、攻略和美景照片，记录旅途中的精彩瞬间和感悟。",
            postsCount = 12543,
            viewsCount = 1456789,
            followersCount = 45678
        ),
        "2" to Topic(
            id = "2",
            name = "美食",
            description = "美食爱好者的天堂，分享美食照片、食谱和餐厅推荐，一起探索舌尖上的美味。",
            postsCount = 34256,
            viewsCount = 2345678,
            followersCount = 78910
        ),
        "3" to Topic(
            id = "3",
            name = "科技",
            description = "关注最新科技动态、产品评测和技术讨论，了解科技如何改变我们的生活。",
            postsCount = 8765,
            viewsCount = 987654,
            followersCount = 23456
        ),
        "4" to Topic(
            id = "4",
            name = "音乐",
            description = "分享你喜爱的音乐、歌手和演唱会体验，探讨音乐的魅力和感染力。",
            postsCount = 15632,
            viewsCount = 1234567,
            followersCount = 34567
        ),
        "5" to Topic(
            id = "5",
            name = "摄影",
            description = "展示你的摄影作品，分享摄影技巧和心得，记录世界的美丽与感动。",
            postsCount = 23487,
            viewsCount = 1876543,
            followersCount = 56789
        )
    )
    
    return topicsMap[topicId] ?: Topic(
        id = topicId,
        name = "话题$topicId",
        description = "这是一个关于话题" + topicId + "的讨论空间，欢迎分享你的见解和经验。",
        postsCount = Random.nextInt(1000, 50000),
        viewsCount = Random.nextInt(50000, 5000000),
        followersCount = Random.nextInt(1000, 100000)
    )
}

/**
 * 生成相关话题
 */
private fun generateRelatedTopics(mainTopic: Topic): List<Topic> {
    val relatedTopicsMap = mapOf(
        "旅行" to listOf("摄影", "美食", "户外", "文化", "酒店"),
        "美食" to listOf("烹饪", "餐厅", "甜点", "健康饮食", "咖啡"),
        "科技" to listOf("编程", "人工智能", "智能手机", "数码产品", "科学"),
        "音乐" to listOf("演唱会", "乐器", "流行音乐", "古典音乐", "音乐创作"),
        "摄影" to listOf("风景摄影", "人像摄影", "手机摄影", "后期处理", "摄影器材")
    )
    
    val relatedNames = relatedTopicsMap[mainTopic.name] ?: listOf(
        "相关话题1",
        "相关话题2",
        "相关话题3",
        "相关话题4",
        "相关话题5"
    )
    
    return relatedNames.mapIndexed { index, name ->
        Topic(
            id = "related-${mainTopic.id}-$index",
            name = name,
            description = "关于" + name + "的讨论",
            postsCount = Random.nextInt(1000, 50000),
            viewsCount = Random.nextInt(50000, 5000000),
            followersCount = Random.nextInt(1000, 100000)
        )
    }
}

/**
 * 生成热门用户
 */
private fun generateTrendingUsers(count: Int): List<User> {
    val names = listOf("张三", "李四", "王五", "赵六", "钱七", "孙八", "周九", "吴十")
    return List(count) { index ->
        User(
            id = "user-$index",
            nickname = names.getOrElse(index) { "用户${index + 1}" },
            postsCount = Random.nextInt(10, 200)
        )
    }
}

/**
 * 生成示例动态
 */
private fun generateSamplePosts(topic: Topic, count: Int): List<Post> {
    val random = Random
    val currentTime = System.currentTimeMillis()
    val dayInMillis = 24 * 60 * 60 * 1000L
    
    val contents = mapOf(
        "旅行" to listOf(
            "刚刚去了云南大理，真的太美了！洱海的水特别清澈，古城也很有韵味。推荐大家有机会一定要去看看。#旅行",
            "分享一个旅行小技巧：出门前做好充分的攻略准备，但也要留一些空间给意外的惊喜和发现。#旅行",
            "在西藏待了一周，震撼于高原的壮美风光和藏族文化的深厚底蕴。这可能是我旅行生涯中最难忘的一次体验！#旅行"
        ),
        "美食" to listOf(
            "今天试着做了云南过桥米线，味道竟然出奇的好！关键是汤底熬得够久，料也要新鲜。#美食",
            "推荐广州的这家点心店，凤爪、虾饺、肠粉都特别地道，价格也很实惠。#美食",
            "自制麻辣香锅的秘诀是什么？我觉得是麻辣料的配比和火候的掌握，大家有什么心得可以分享吗？#美食"
        ),
        "科技" to listOf(
            "体验了最新的AR眼镜，沉浸感很强，但续航还是个问题。期待技术能再进一步发展。#科技",
            "关于人工智能伦理问题的思考：我们应该如何平衡技术发展和潜在风险？#科技",
            "新款手机的相机真的提升了不少，尤其是夜景模式，几乎可以媲美专业相机了。#科技"
        ),
        "音乐" to listOf(
            "周杰伦的新歌听了吗？感觉回到了《七里香》的时代，满满的青春回忆。#音乐",
            "学习吉他一年了，终于能流畅弹唱一些简单的歌曲。坚持真的很重要！#音乐",
            "昨晚去看了交响乐演出，现场的氛围和感染力是录音无法比拟的。强烈推荐亲身体验！#音乐"
        ),
        "摄影" to listOf(
            "分享一张今天拍的日落照片，没用任何滤镜，大自然的色彩就是最美的！#摄影",
            "聊聊我对构图的理解：有时候最简单的构图反而最有力量，不必刻意追求复杂。#摄影",
            "入手了新的广角镜头，风景摄影的视野一下子开阔了很多，这次旅行收获满满。#摄影"
        )
    )
    
    val topicContents = contents[topic.name] ?: listOf(
        "关于" + topic.name + "的一些想法和感受...",
        "分享一下我在" + topic.name + "领域的经验和见解...",
        "对" + topic.name + "有什么看法？欢迎大家一起讨论！"
    )
    
    val users = listOf("张三", "李四", "王五", "赵六", "钱七", "孙八", "周九", "吴十")
    
    return List(count) { index ->
        val randomUserIndex = random.nextInt(users.size)
        Post(
            id = "${topic.id}-post-$index",
            authorId = "user-$randomUserIndex",
            authorName = users[randomUserIndex],
            content = topicContents[random.nextInt(topicContents.size)],
            timestamp = currentTime - random.nextLong(1, 30) * dayInMillis / random.nextInt(1, 30),
            likes = random.nextInt(5, 500),
            comments = random.nextInt(0, 100),
            shares = random.nextInt(0, 50),
            hasImage = random.nextBoolean(),
            isLiked = random.nextBoolean()
        )
    }
} 